import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

/**
 * @class
 * @description
 * 超文本引用地址链接的基本格式
 * https://youdao.com/result?word=hypertext%20reference&lang=en

  https:// scheme协议
  youdao.com HOST:PORT 主机名:端口

  /result path 路径

  /parse/classes/oid

  /parse/classes/<className>?where={"objectId",<oid>}


  ?word=hypertext%20reference&lang=en
  查询参数 query search params
  queryParams
  queryParamsMap %20 空格
  {"word":"hypertext reference","lang":"en"}
 */
@Injectable({
  providedIn: 'root'
})
export class CurrentRouteService {

  queryParamMap:Observable<any> = new Observable<any>(observer => {
    observer.next({
      params:this.getQueryParamMap()
    });
  });
  
  constructor() { }
  /**
   * @function
   * @returns - 当前的页面地址
   * @description
   * Href - Hypertext Reference
   * location.href location是网页中定位导航的库.href方法
   * 早期网页叫做：Hypertext 超文本
   */
  getHref(){
    return location.href
  }

  /**
   * @function
   * @returns 查询参数的MAP映射表
   */
  getQueryParamMap(){
    let paramsMap:any = {}
    // location.search打印值 '?word=hypertext%20reference&lang=en'
    location.search.split("?")[1] // "word=hypertext%20reference&lang=en"
    location.search.split("?")[1].split("&") // ["word=hypertext%20reference","lang=en"]
    location.search.split("?")[1].split("&").forEach(text=>{
      let key = text.split("=")[0]
      let value = text.split("=")[1] // {"word":"hypertext%20reference","lang"="en"}
      // https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent
      // https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
      // 对URI中加密转码的字符串，进行解码
      value = decodeURIComponent(value) // {"word":"hypertext reference","lang"="en"}
      paramsMap[key] = value
    })
    console.log(paramsMap) 
    // 将打印值转成 {"word":"hypertext reference","lang":"en"}
    return paramsMap
  }
}
